package iface.aplicativo.negocio.aps;

import iface.aplicativo.integracao.dao.FabricanteDAO;
import iface.aplicativo.negocio.entidade.Fabricante;
import iface.arquitetura.utilitario.ReportUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true, rollbackFor = { Exception.class })
public class RelatorioTesteAPS {

	@Resource
	FabricanteDAO fabricanteDAO;

	private Collection<Fabricante> fabricantes;

	public void gerarRelatorioFabricantes() {
		try {
			this.fabricantes = this.fabricanteDAO.consultar();

			HttpServletResponse response = (HttpServletResponse) FacesContext
					.getCurrentInstance().getExternalContext().getResponse();

			byte[] relatorioEmByte = new ReportUtils().obterRelatorioPDF(
					"fabricantes.jasper",
					(HashMap<String, Object>) definirParametrosRelatorios(),
					this.fabricantes);

			new ReportUtils().escreveNoResponsePDF(relatorioEmByte,
					"fabricantes", response);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Map<String, Object> definirParametrosRelatorios() throws Exception {
		HashMap<String, Object> parametrosReport = new HashMap<String, Object>();
		parametrosReport.put("SUBREPORT_DIR", "" + "/");

		return parametrosReport;
	}

}
